package com.desire.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;


/**
 * @author desire
 */
public class _22_nio测试 {

    public static void main(String[] args) throws IOException {

    }

    public static void main2(String[] args) throws IOException {
        //写入文件 , StandardOpenOption.APPEND参数是追加模式
        try (var out = Files.newBufferedWriter(
                Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\file.txt"),
                StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
            out.write("hello 中文234234\n");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // nio 删除文件
        // Files.delete(Paths.get(""));
        // Files.deleteIfExists(Paths.get(""));

        // 文件复制
        // Files.copy(Paths.get(""), Paths.get(""));
        // Paths.get("");

        // nio 更名，移动文件(剪切粘贴文件)
        // Files.move(Paths.get(""), Paths.get(""));

        try (Stream<Path> stream = Files.list(Paths.get(""))) {
            Iterator<Path> iterator = stream.iterator();
            while (iterator.hasNext()) {
                Path next = iterator.next();
                System.out.println(next.getFileName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * nio 递归删除非空目录
     *
     * @param path
     */
    public static void deldir(Path path) {
        if (Files.isDirectory(path)) {
            try (var dirs = Files.newDirectoryStream(path)) {
                for (Path p : dirs) {
                    if (Files.isDirectory(p)) {
                        deldir(p);
                    } else {
                        Files.delete(p);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main3(String[] args) throws IOException {

        // Path用于来表示文件路径和文件

        // final类Paths的两个static方法，如何从一个路径字符串来构造Path对象：

        // 获取路径
        Path path = Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\", "abcu.txt");
        out.println(path);

        // FileSystems构造：
        Path path3 = FileSystems.getDefault().getPath("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\", "abcu.txt");
        out.println(path3);


        createFile("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\u.txt");

        // 读取文件
        String collect = Files.newBufferedReader(Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\srcjava.txt")).lines().collect(Collectors.joining("\n"));
        // System.out.println(collect);

        // 读取字符文件
        var p = Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\srcjava.txt");
        List<String> text = Files.readAllLines(p);
        //text.forEach(System.out::println);

        // 读取字节文件
        byte[] bytes = Files.readAllBytes(Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\ok.png"));
        // 将读取到的字节写入到一个文件当中
        Files.write(Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\ok2.png"), bytes);

        out.println(bytes.length);

        try (BufferedReader reader = Files.newBufferedReader(Paths.get("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\file.txt"), StandardCharsets.UTF_8)) {
            String str = null;
            while ((str = reader.readLine()) != null) {
                out.println(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void createFile(String path) {
        Path pt = Paths.get(path);
        if (Files.exists(pt)) {
            out.println("文件已存在");
        } else {
            try {
                Files.createFile(pt);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
